﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EraNet.Core;
using EraNet.Core.Model.Messages;
using EraNet.Core.Data;
using EraNet.Core.Caching;
using EraNet.Core.Model.Enum;

namespace EraNet.Services.Messages
{
    public class NoticeServices : INoticeServices
    {
        #region Constants
        private const string NOTICES_BY_ID_KEY = "EraNet.Notice.Id-{0}";
        #endregion
        #region Ctor
        private readonly IRepository<Notices> _noticeRepository;
        private readonly ICacheManager _cacheManager;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="adminRepository"></param>
        /// <param name="cacheManager"></param>
        public NoticeServices(IRepository<Notices> noticeRepository, ICacheManager cacheManager)
        {
            _noticeRepository = noticeRepository;
            _cacheManager = cacheManager;
        }
        #endregion
        #region Methods
        /// <summary>
        /// 插入新的输入据
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual int Insert(Notices model)
        {
            if (model == null) throw new ExtException("NUll Notices Model");
            int rs = _noticeRepository.Insert(model);
            return rs;
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual int Update(Notices model)
        {
            if (model == null) throw new ExtException("Null Notices");
            return _noticeRepository.Update(model) > 0 ? 1 : 0;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(Notices model)
        {
            throw new NotImplementedException();
        }

        public virtual Notices GetNoticeById(int noticeId)
        {
            if (noticeId <= 0) return null;
            string key = string.Format(NOTICES_BY_ID_KEY, noticeId);
            return _cacheManager.Get(key, () =>
            {
                var pv = _noticeRepository.GetById(noticeId);
                return pv;
            });
        }

        /// <summary>
        /// 根据ID获取公告信息
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="noticeId"></param>
        /// <returns></returns>
        public virtual Notices GetNoticeById(int noticeId, int schoolId)
        {
            if (noticeId <= 0) return null;

            var query = _noticeRepository.Table;
            string ids = string.Format("[{0}]", schoolId);
            query = query.Where(c => c.AcceptId.Contains(ids));

            var pv = query.Where(c => c.Id == noticeId).FirstOrDefault();
            return pv;
        }
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">分页大小</param>
        /// <param name="PageCount">总页数</param>
        /// <returns></returns>
        public virtual IPagedList<Notices> SelectPager(int pageIndex, int pageSize)
        {
            var query = _noticeRepository.Table;
            query = query.OrderBy(c => c.CreateDate);
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            var list = new PagedList<Notices>(query, pageIndex, pageSize);
            return list;
        }
        /// <summary>
        /// 根据接收ID，分页获取数据
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="SchoolId">驾校ID</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">分页大小</param>
        /// <param name="PageCount">总页数</param>
        /// <returns></returns>
        public virtual IPagedList<Notices> SelectPager( int schoolId,int pageIndex, int pageSize)
        {
            var query = _noticeRepository.Table;
            string ids = string.Format("[{0}]", schoolId);
            query = query.Where(n => n.AcceptId.Contains(ids)&&n.NType1==(int)NType1.SystemNotice &&(n.NType2==(int)NType2.SchoolAndStudent||n.NType2==(int)NType2.SendToSchool));
            query = query.Where(n => n.StartDate > DateTime.Now && n.EndDate <= DateTime.Now);
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            query = query.OrderBy(c => c.CreateDate);
            var list = new PagedList<Notices>(query, pageIndex, pageSize);
            return list;
        }

                /// <summary>
        /// 根据接收ID，分页获取数据
        /// </summary>
        /// <remarks>
        /// author:daivd
        /// time  :2013-6-8
        /// </remarks>
        /// <param name="SchoolId">驾校ID</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">分页大小</param>
        /// <param name="PageCount">总页数</param>
        /// <returns></returns>
        public virtual IPagedList<Notices> SelectPager(int sender,int userId,int pageIndex, int pageSize)
        {
            var query = _noticeRepository.Table;
            query = query.Where(c => c.NType2 == (int)NType2.SendToStudent || c.NType2 == (int)NType2.SchoolAndStudent);
            string ids = string.Format("{0}", userId);
            query = query.Where(n => n.AcceptId.Contains(ids) || n.AcceptId == null);
            query = query.Where(c => (c.CreaterId == sender&&c.SchoolId==0)||c.SchoolId==sender);
            query = query.Where(n => n.StartDate <= DateTime.Now && n.EndDate >= DateTime.Now);
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            query = query.OrderByDescending(c => c.CreateDate);
            var list = new PagedList<Notices>(query, pageIndex, pageSize);
            return list;
        }
        #endregion

    }
}
